package com.boot.web.controller;

import com.boot.web.http.HttpModel;
import com.boot.web.util.HttpResponseUtil;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description Web基本应用接口
 * @date 2020/3/10 19:50
 **/
@Slf4j
@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Value("${spring.application.name}")
    private String applicationName;
    private final Counter visitCounter;

    public ApplicationController(MeterRegistry registry) {
        visitCounter = Counter.builder("application_welcome_counter")
                .tag("api", "welcome")
                .description("Number of visits to the api")
                .register(registry);

        //ExecutorServiceMetrics.monitor(registry, Executors.newSingleThreadExecutor(), "application_executor");
    }

    @Timed(value = "application_welcome_rt", description = "RT", histogram = true)
    @GetMapping("/welcome")
    public ResponseEntity<HttpModel<Object>> welcome() {
        visitCounter.increment();
        return HttpResponseUtil.OK("Welcome to " + applicationName + '!');
    }

}
